import Cookies from 'js-cookie'
/* 获取cookie 服务端需要传入request */
export default function (req) {
  if (req && process.server) {
    let Cookies = {}
    if (req.headers.cookie != null) {
      req.headers.cookie.split(';').forEach(l => {
        let parts = l.split('=')
        Cookies[parts[0].trim()] = (parts[1] || '').trim()
      })
      /* let arr = req.headers.cookie.split('=')
      Cookies = JSON.parse(decodeURIComponent(arr[1])) */
      if (Cookies.vuex) return JSON.parse(decodeURIComponent(Cookies.vuex))
    }
    return Cookies
  } else {
    return Cookies.getJSON('vuex') ? Cookies.getJSON('vuex') : {}
  }
}
